package arrival;

import dbhandler.*;
import common.*;
import evthandler.EventMessage;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class AddBooking {

    private Guest guest;
    private Booking booking;
    private boolean evt;

    /**
     *
     * @param parent
     */
    public AddBooking(java.awt.Frame parent) {
        evt = false;
        guest = addGuest();
        if(guest != null){
            booking = createBooking();
            if(booking != null){
                sendBooking();
            }
        }
        if (guest == null) {
            while (evt == false) {
                evt = new EventMessage("Cancel?", "Do you want to cancel the booking?").showYesNoConfirm();
                if (evt == false) {
                    guest = addGuest();
                    if(guest != null){
                        booking = createBooking();
                        if(booking != null){
                        sendBooking();
                        }
                    }
                }
                if (evt == true) {
                    new EventMessage("Cancelled", "Booking cancelled!").showMessage();
                    new MainDialog(parent, true);
                }
            }
        }
    }

    /**
     *
     * @return
     */
    private Guest addGuest() {
        Guest thisGuest = new ExistingGuestDialog(null, true).returnGuest();
        return thisGuest;
    }

    /**
     *
     * @return
     */
    private Booking createBooking(){
        Booking thisBooking = new BookingDialog(null, true).getBooking();
        return thisBooking;
    }

    /**
     *
     */
    private void sendBooking() {
        boolean confirm;
        booking.setGuest(guest);
        confirm = booking.insert();
        if(confirm){
            new EventMessage("Confirmed", "The booking has been created!").showMessage();
        }
        else{
            new EventMessage("Error", "The booking was not created!").showMessage();
        }
    }
}
